package com.itshops.controller;

import com.itshops.entity.Address;
import com.itshops.entity.Cart;
import com.itshops.entity.Orders;
import com.itshops.entity.User;
import com.itshops.service.AddressService;
import com.itshops.service.CartService;
import com.itshops.service.OrderService;
import com.itshops.service.impl.AddressServiceImpl;
import com.itshops.service.impl.CartserviceImpl;
import com.itshops.service.impl.OrderServiceImpl;
import com.itshops.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseServlet{
    OrderService orderService=new OrderServiceImpl();
    //显示订单预览
    public String preView(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {

        //获取请求参数
        String uid = req.getParameter("uid");
        //调用业务逻辑获取地址和购物车
        AddressService addressService =new AddressServiceImpl();
        List<Address> addressList = addressService.findAll(Integer.parseInt(uid));
        CartService cartService=new CartserviceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));
        //响应,将数据提供给前端
        req.setAttribute("addressList",addressList);
        req.setAttribute("cartList",cartList);
        return Constants.FORWARD+"/order.jsp";
    }
    //生成订单
    public String create(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {

        //获取参数
        String uid = req.getParameter("uid");
        String aid = req.getParameter("aid");
        String sum = req.getParameter("sum");
        //调用业务逻辑
        orderService.crateOrder(aid,uid,sum);
        //响应
        return Constants.FORWARD+"/order?method=show";
    }
    //显示订单
    public String show(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        User user = (User) req.getSession().getAttribute("loginUser");

        if (user==null){
            req.getSession().setAttribute("msg","请先登录!");
            return Constants.FORWARD+"/login.jsp";
        }
        //调用业务逻辑
        List<Orders> ordersList=orderService.fianAll(user.getUid());
        //响应
        req.setAttribute("ordersList",ordersList);
        return Constants.FORWARD+"/orderList.jsp";
    }

    public String detail(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {

        String oid = req.getParameter("oid");
        //调用业务
        Orders order=orderService.findOrderByoid(oid);
        //响应
        req.setAttribute("order",order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }

    //支付商品
    public String PayFor(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        //获取订单id参数
        String oid = req.getParameter("oid");
        //调用业务
        orderService.updateOrderByOid(oid);
        //响应
        return Constants.FORWARD+"/order?method=show";
    }
}
